package com.store.pet.petstore.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.droidbyme.dialoglib.DroidDialog;
import com.gyf.barlibrary.ImmersionBar;
import com.store.pet.petstore.R;
import com.store.pet.petstore.adapter.CommodityAdapter;
import com.store.pet.petstore.bean.CommodityBean;
import com.store.pet.petstore.bean.MessageBean;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.GsonUtils;
import com.store.pet.petstore.util.OKHttpUtils;
import com.store.pet.petstore.util.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.store.pet.petstore.fragment.PersonFragment.ADD_COMMODITY;

public class MyCommodityActivity extends BaseActivity {

    @BindView(R.id.my_status)
    View myStatus;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycler_commodity)
    RecyclerView recyclerCommodity;

    public static final int TASK_SUCCESS = 0x0002;

    public static final int UPDATE_SUCCESS = 0x0008;

    private CommodityAdapter adapter;

    private List<CommodityBean.ContentBean.PetlistBean> list = new ArrayList<>();

    private DroidDialog dialog;  //删除商品的弹窗

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_my_commodity;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        //沉浸式标题栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        takeLinearStatusBar(myStatus);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.fanhui);  //设置左上角图标
        toolbar.setNavigationOnClickListener(view -> {
            setResult(ADD_COMMODITY,new Intent());
            finish();
        });  //退出

        recyclerCommodity.setLayoutManager(new LinearLayoutManager(this));
        recyclerCommodity.addItemDecoration(new SpaceItemDecoration(30));
        adapter = new CommodityAdapter(R.layout.item_commodity_rv,list);
        recyclerCommodity.setAdapter(adapter);
        /**
         * 长按删除
         */
        adapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
            @Override
            public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
                //删除商品弹窗
                dialog = new DroidDialog.Builder(MyCommodityActivity.this)
                        .title("温馨提示")
                        .content("是否确认删除商品？")
                        .cancelable(false, false)
                        .positiveButton("确定", droidDialog -> {
                            Map<String,Object> map = new HashMap<>();
                            map.put("petid",list.get(position).getId());
                            OKHttpUtils.requestData(Constant.DELETE_COMMODITY, map, new OKHttpUtils.ServerCallBack() {
                                @Override
                                public void onSuccess(String data) {
                                    MessageBean msg = GsonUtils.parseMessage(data);
                                    if(msg.getCode() == 107){
                                        loadData();
                                    }
                                    ToastUtils.showShort(msg.getMsg());
                                    dialog.dismiss();
                                }

                                @Override
                                public void onFailed(String message) {
                                    dialog.dismiss();
                                }
                            });

                        })
                        .negativeButton("取消", droidDialog -> dialog.dismiss()).show();
                return true;
            }
        });
        /**
         * 点击编辑
         */
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.commodity_item:
                        Intent intent = new Intent(MyCommodityActivity.this,EditCommodityActivity.class);
                        intent.putExtra("isEdit",true);
                        intent.putExtra("commodity",list.get(position));
                        startActivityForResult(intent,UPDATE_SUCCESS);
                        break;
                }
            }
        });

    }

    @Override
    public void loadData() {
        list.clear();
        Map<String,Object> map = new HashMap<>();
        map.put("sellerid",getMyId());
        OKHttpUtils.requestData(Constant.QUERY_COMMODITY, map, new OKHttpUtils.ServerCallBack() {
            @Override
            public void onSuccess(String data) {
                CommodityBean bean = GsonUtils.parseCommodityBean(data);
                if(bean.getContent().getPetlist().size() != 0){
                    list.addAll(bean.getContent().getPetlist());
                }
                /**
                 * 在主线程刷新UI
                 */
                new Thread(() -> runOnUiThread(() -> adapter.notifyDataSetChanged())).start();

            }

            @Override
            public void onFailed(String message) {
                ToastUtils.showShort("查询失败，请稍后再试");
            }
        });
    }

    @OnClick(R.id.add)
    public void onViewClicked() {
        Intent intent = new Intent(MyCommodityActivity.this,EditCommodityActivity.class);
        startActivityForResult(intent,TASK_SUCCESS);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(data != null){
            switch (resultCode){
                case TASK_SUCCESS:
                    loadData();
                    break;
                case UPDATE_SUCCESS:
                    loadData();
                    break;
            }
        }
    }
}
